<template>
    <div class="app-container">
        <!-- 记录状态 -->
        <el-row :gutter="24" style="color: #9bb1e9;" class="mb15">
            <el-col :span="1.5">
                状态
            </el-col>
            <el-col :span="1.5" :class="isActive('all')" @click="changeStatus('all')">
                全部
            </el-col>
            <el-col :span="1.5" :class="isActive('public')" @click="changeStatus('public')">
                公开
            </el-col>
            <el-col :span="1.5" :class="isActive('secret')" @click="changeStatus('secret')">
                私密
            </el-col>
            <el-col :span="1.5" :class="isActive('draft')" @click="changeStatus('draft')">
                草稿
            </el-col>
            <el-col :span="1.5" :class="isActive('delete')" @click="changeStatus('delete')">
                回收站
            </el-col>
        </el-row>
        <!-- 搜索栏 -->
        <el-form :model="queryParams" :inline="true" v-show="showSearch">
            <el-form-item label="主题">
                <el-input @keyup.enter="handleQuery" v-model="queryParams.keyword" style="width: 150px"
                          placeholder="请输入记录主题" clearable/>
            </el-form-item>
            <el-form-item label="类型">
                <el-select v-model="queryParams.type" placeholder="请选择类型" clearable style="width: 130px;">
                    <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"/>
                </el-select>
            </el-form-item>
            <el-form-item label="标签">
                <el-select v-model="queryParams.tagId" placeholder="请选择标签" clearable filterable
                           @visible-change="getTag"
                           style="width: 130px">
                    <el-option v-for="item in tagList" :key="item.id" :label="item.tagName" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            </el-form-item>
        </el-form>
        <!-- 头部操作按钮 -->
        <el-row :gutter="10" class="mb15">
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="recordIdList.length === 0"
                           @click="handleDelete(undefined)">批量删除
                </el-button>
            </el-col>
            <el-col :span="1.5" v-if="queryParams.isDelete == 0">
                <el-button type="danger" plain icon="Delete" :disabled="recordIdList.length === 0"
                           @click="handleRecycle(undefined)">批量回收
                </el-button>
            </el-col>
            <el-col :span="1.5" v-if="queryParams.isDelete == 1">
                <el-button type="success" plain icon="Finished" :disabled="recordIdList.length === 0"
                           @click="handleRecycle(undefined)">批量恢复
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!-- 表格展示 -->
        <el-table :data="recordList" @selection-change="handleSelectionChange" v-loading="loading"
                  border height="500" :default-sort="{ prop: 'createTime', order: 'descending' }">
            <!-- 表格列 -->
            <el-table-column type="selection" width="55" align="center"></el-table-column>
            <!--缩略图-->
            <el-table-column prop="cover" label="缩略图" width="200" align="center">
                <template #default="scope">
                    <el-image class="record-cover" :src="scope.row.cover"></el-image>
                    <el-icon v-if="scope.row.status == 1" class="record-status-icon">
                        <View/>
                    </el-icon>
                    <el-icon v-if="scope.row.status == 2" class="record-status-icon">
                        <Hide/>
                    </el-icon>
                    <el-icon v-if="scope.row.status == 3" class="record-status-icon">
                        <EditPen/>
                    </el-icon>
                </template>
            </el-table-column>
            <!-- 记录标题 -->
            <el-table-column prop="title" label="主题" align="center"></el-table-column>
            <!-- 记录标签 -->
            <el-table-column prop="tagVOList" label="标签" width="160" align="center" resizable>
                <template #default="scope">
                    <el-tag v-for="item of scope.row.tagVOList" :key="item.tagId"
                            style="margin-right: 0.2rem; margin-top: 0.2rem">
                        {{ item.tagName }}
                    </el-tag>
                </template>
            </el-table-column>
            <!-- 记录浏览量 -->
            <el-table-column prop="viewCount" label="浏览量" width="80" align="center" sortable>
            </el-table-column>
            <!-- 记录点赞量 -->
            <el-table-column prop="likeNumber" label="点赞数" width="80" align="center" sortable>
            </el-table-column>
            <!-- 记录收藏量 -->
            <el-table-column prop="collectNumber" label="收藏数" width="80" align="center" sortable>
            </el-table-column>
            <!-- 记录类型 -->
            <el-table-column prop="type" label="类型" width="70" align="center">
                <template #default="scope">
                    <el-tag v-if="scope.row.type == 1" type="warning">约会</el-tag>
                    <el-tag v-if="scope.row.type == 2" type="success">活动</el-tag>
                    <el-tag v-if="scope.row.type == 3" type="danger">赛事</el-tag>
                    <el-tag v-if="scope.row.type == 4" type="info">帮助</el-tag>
                </template>
            </el-table-column>
            <!-- 记录置顶 -->
            <el-table-column prop="isTop" label="置顶" width="70" align="center">
                <template #default="scope">
                    <el-switch v-model="scope.row.isTop" style="--el-switch-on-color: #13ce66;"
                               :disabled="scope.row.isDelete == 1" :active-value="1" :inactive-value="0"
                               @change="handleTop(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <!-- 记录推荐 -->
            <el-table-column prop="isRecommend" label="推荐" width="70" align="center">
                <template #default="scope">
                    <el-switch v-model="scope.row.isRecommend" style="--el-switch-on-color: #13ce66;"
                               :disabled="scope.row.isDelete == 1" :active-value="1" :inactive-value="0"
                               @change="handleRecommend(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <!-- 发表人 -->
            <el-table-column prop="userId" label="用户ID" align="center" width="70"></el-table-column>
            <!-- 创建时间 -->
            <el-table-column prop="createTime" width="130" label="创建时间" align="center" sortable>
                <template #default="scope">
                    <div class="create-time">
                        <el-icon>
                            <clock/>
                        </el-icon>
                        <span style="margin-left: 10px">{{ formatDate(scope.row.createTime) }}</span>
                    </div>
                </template>
            </el-table-column>
            <!-- 操作 -->
            <el-table-column width="80" label="操作" align="center" fixed="right">
                <template #default="scope">
                    <el-button type="primary" icon="Edit" link @click="handleEdit(scope.row.id)"
                               v-if="scope.row.isDelete == 0">
                        编辑
                    </el-button>
                    <el-button style="margin-left: 0" type="danger" icon="Delete" link @click="handleDelete(scope.row.id)">
                        删除
                    </el-button>
                    <el-button style="margin-left: 0" type="danger" icon="Delete" link @click="handleRecycle(scope.row.id)"
                               v-if="queryParams.isDelete == 0">
                        回收
                    </el-button>
                    <el-button style="margin-left: 0" type="success" icon="Finished" link @click="handleRecycle(scope.row.id)"
                               v-if="queryParams.isDelete == 1">
                        恢复
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination v-if="count > 0" :total="count" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize" @pagination="getList"/>
    </div>
</template>

<script setup lang="ts">
import {
    deleteRecord, getQTagOption,
    getRecordList,
    getTagOption,
    recycleRecord,
    updateRecordRecommend,
    updateRecordTop
} from '@/api/record';
import {Record, RecordQuery, DeleteDTO, TagVO} from '@/api/record/types';
import router from "@/router";
import {formatDate} from "@/utils/date";
import {messageConfirm, notifySuccess} from '@/utils/modal';
import {computed, onMounted, reactive, toRefs} from 'vue';

const isActive = computed(() => {
    return function (value: string | undefined) {
        return activeStatus.value == value ? "active-status" : "status";
    }
});
const data = reactive({
    count: 0,
    showSearch: true,
    loading: false,
    title: "",
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        isDelete: 0,
    } as RecordQuery,
    typeList: [
        {
            value: 1,
            label: "约会",
        },
        {
            value: 2,
            label: "活动",
        },
        {
            value: 3,
            label: "赛事",
        },
        {
            value: 4,
            label: "帮助",
        }
    ],
    activeStatus: "all",
    tagList: [] as TagVO[],
    recordIdList: [] as number[],
    recordList: [] as Record[]
});
const {
    count,
    showSearch,
    loading,
    queryParams,
    typeList,
    activeStatus,
    tagList,
    recordIdList,
    recordList
} = toRefs(data);
const handleSelectionChange = (selection: Record[]) => {
    recordIdList.value = selection.map((item) => item.id);
};
const changeStatus = (status: string) => {
    switch (status) {
        case "all":
            queryParams.value.isDelete = 0;
            queryParams.value.status = undefined;
            break;
        case "public":
            queryParams.value.isDelete = 0;
            queryParams.value.status = 1;
            break;
        case "secret":
            queryParams.value.isDelete = 0;
            queryParams.value.status = 2;
            break;
        case "draft":
            queryParams.value.isDelete = 0;
            queryParams.value.status = 3;
            break;
        case "delete":
            queryParams.value.isDelete = 1;
            queryParams.value.status = undefined;
            break;
    }
    activeStatus.value = status;
    handleQuery();
};
const handleRecycle = (id?: number) => {
    let params: DeleteDTO = {
        idList: [],
        isDelete: 0,
    };
    if (id != undefined) {
        params.idList = [id];
    } else {
        params.idList = recordIdList.value;
    }
    params.isDelete = queryParams.value.isDelete === 0 ? 1 : 0;
    let text = queryParams.value.isDelete === 0 ? "回收" : "恢复";
    messageConfirm("确认" + text + "已选中的数据项?").then(() => {
        recycleRecord(params).then(({data}) => {
            if (data.flag) {
                notifySuccess(data.msg);
                getList();
            }
        });
    }).catch(() => {
    });
};
const handleEdit = (id: number) => {
    router.push({path: `/record/writer/${id}`});
};
const handleDelete = (id?: number) => {
    let ids: number[] = [];
    if (id == undefined) {
        ids = recordIdList.value;
    } else {
        ids = [id];
    }
    messageConfirm("确认删除已选中的数据项?").then(() => {
        deleteRecord(ids).then(({data}) => {
            if (data.flag) {
                notifySuccess(data.msg);
                getList();
            }
        });
    }).catch(() => {
    });
};
const handleTop = (record: Record) => {
    let text = record.isTop === 0 ? "取消置顶" : "置顶";
    messageConfirm("确定要" + text + "该记录吗?").then(() => {
        updateRecordTop({id: record.id, isTop: record.isTop}).then(({data}) => {
            if (data.flag) {
                notifySuccess(data.msg);
            } else {
                record.isTop = record.isTop === 0 ? 1 : 0;
            }
        });
    }).catch(() => {
        record.isTop = record.isTop === 0 ? 1 : 0;
    });
};
const handleRecommend = (record: Record) => {
    let text = record.isRecommend === 0 ? "取消推荐" : "推荐";
    messageConfirm("确定要" + text + "该记录吗?").then(() => {
        updateRecordRecommend({id: record.id, isRecommend: record.isRecommend}).then(({data}) => {
            if (data.flag) {
                notifySuccess(data.msg);
            } else {
                record.isRecommend = record.isRecommend === 0 ? 1 : 0;
            }
        });
    }).catch(() => {
        record.isRecommend = record.isRecommend === 0 ? 1 : 0;
    });
};
const getTag = (val: boolean) => {
    if (val) {
        getQTagOption().then(({data}) => {
            tagList.value = data.data;
        });
    }
};
const getList = () => {
    loading.value = true;
    getRecordList(queryParams.value).then(({data}) => {
        recordList.value = data.rows;
        count.value = data.total;
        loading.value = false;
    })
};
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};
onMounted(() => {
    getList();
});
</script>

<style scoped>
.status {
    cursor: pointer;
}

.active-status {
    cursor: pointer;
    color: rgba(80, 166, 31, 0.8);
    font-weight: bold;
}

.record-cover {
    position: relative;
    width: 100%;
    height: 80px;
    border-radius: 5px;
}

.record-cover::after {
    content: "";
    background: rgba(0, 0, 0, 0.3);
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

.record-status-icon {
    color: #e8eaf1;
    font-size: 1rem;
    position: absolute;
    right: 1rem;
    bottom: 1rem;
}
</style>